﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using RushTracker.Core.Models;
using RushTracker.Core.Models.Torrents;

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute { }
}


namespace RushTracker.Web.UI.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : RushTrackerController
    {
        public ActionResult ShowRoles()
        {
            ViewData.Model = DataRepository.GetAll<Role>();
            return View();
        }

        public ActionResult ShowCategories()
        {
            ViewData.Model = DataRepository.GetTopLevelCategories();
            return View();
        }

        public ActionResult ShowMetaFields()
        {
            ViewData.Model = DataRepository.GetAll<MetaField>();
            return View();
        }

        public ActionResult EditCategory(int catId)
        {
            ViewData.Model = DataRepository.GetObjectById<Category>(catId);

            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Admin/Controls/CategoryDetails.ascx");
            }

            return View();
        }

        // POST
        [HttpPost]
        public ActionResult EditCategory(int catId, FormCollection form)
        {
            Category c = DataRepository.GetObjectById<Category>(catId);

            c.Name = form["categoryname"].Trim();
            c.Description = form["categorydescription"].Trim();
            c.IconUrl = form["categoryiconurl"].Trim();

            // mess with metafields
            
            int parentid = Convert.ToInt32(form["categoryparent"]);

            if (parentid > 0)
            {
                Category parent = DataRepository.GetObjectById<Category>(parentid);
                c.Parent = parent;
            }

            DataRepository.UpdateObject(c);
            DataRepository.Flush();

            return RedirectToAction("ShowCategories");
        }

        // GET
        public ActionResult EditRole(int roleId)
        {
            ViewData.Model = DataRepository.GetObjectById<Role>(roleId);

            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Admin/Controls/EditRoleForm.ascx");
            }

            return View();
        }

        // POST
        [HttpPost]
        public ActionResult EditRole(int roleId, FormCollection form)
        {
            Role r = DataRepository.GetObjectById<Role>(roleId);
            r.IconUrl = form["roleiconurl"].Trim();

            DataRepository.UpdateObject(r);
            DataRepository.Flush();

            return RedirectToAction("ShowRoles");
        }

        // GET
        public ActionResult EditMetaField(int metaFieldId)
        {
            ViewData.Model = DataRepository.GetObjectById<MetaField>(metaFieldId);

            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Admin/Controls/EditMetaFieldForm.ascx");
            }

            return View();
        }

        // POST
        [HttpPost]
        public ActionResult EditMetaField(int metaFieldId, FormCollection form)
        {
            MetaField mf = DataRepository.GetObjectById<MetaField>(metaFieldId);

            mf.DataType = form["metafielddatatype"].Trim();
            mf.DefaultValue = form["metafielddefaultvalue"].Trim();
            mf.Name = form["metafieldname"].Trim();
            mf.ViewPath = form["metafieldviewpath"].Trim();

            DataRepository.UpdateObject(mf);
            DataRepository.Flush();

            return RedirectToAction("ShowMetaFields");
        }

        // GET
        public ActionResult CreateRole()
        {
            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Admin/Controls/CreateRoleForm.ascx");
            }

            return View();
        }

        // POST
        [HttpPost]
        public ActionResult CreateRole(FormCollection form)
        {
            Role r = new Role();
            r.Name = form["rolename"].Trim();
            r.IconUrl = form["roleiconurl"].Trim();

            DataRepository.SaveObject(r);
            DataRepository.Flush();

            return RedirectToAction("ShowRoles");
        }

        // GET
        public ActionResult CreateCategory()
        {
            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Admin/Controls/CreateCategoryForm.ascx");
            }

            return View();
        }

        // POST
        [HttpPost]
        public ActionResult CreateCategory(FormCollection form)
        {
            Category c = new Category();
            c.Description = form["categorydescription"].Trim();
            c.IconUrl = form["categoryiconurl"].Trim();
            c.Name = form["categoryname"].Trim();

            if (!String.IsNullOrEmpty(form["metafieldlist"]))
            {
                string[] fields = form["metafieldlist"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string f in fields)
                {
                    MetaField mf = DataRepository.GetObjectById<MetaField>(Convert.ToInt32(f));
                    c.MetaFields.Add(mf);
                }
            }

            int parentid = Convert.ToInt32(form["categoryparent"]);

            if (parentid > 0)
            {
                Category parent = DataRepository.GetObjectById<Category>(parentid);
                c.Parent = parent;
            }

            DataRepository.SaveObject(c);
            DataRepository.Flush();

            return RedirectToAction("ShowCategories");
        }

        // GET
        public ActionResult CreateMetaField()
        {
            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Admin/Controls/CreateMetaFieldForm.ascx");
            }

            return View();
        }

        // POST
        [HttpPost]
        public ActionResult CreateMetaField(FormCollection form)
        {
            MetaField mf = new MetaField();
            mf.DataType = form["metafielddatatype"].Trim();
            mf.DefaultValue = form["metafielddefaultvalue"].Trim();
            mf.Name = form["metafieldname"].Trim();
            mf.ViewPath = form["metafieldviewpath"].Trim();

            DataRepository.SaveObject(mf);
            DataRepository.Flush();

            return RedirectToAction("ShowMetaFields");
        }
    }
}
